/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opensudoku.root.concrete;

import java.util.Vector;

import org.opensudoku.root.exceptions.ConcreteNotNeededException;
import org.opensudoku.root.exceptions.InvalidBoxEntryException;
import org.opensudoku.root.exceptions.InvalidDimensionException;
import org.opensudoku.root.exceptions.NoMorePossibilitiesException;
import org.opensudoku.root.exceptions.UnIntializedParameterException;
import org.opensudoku.root.fon.FonResults;
import org.opensudoku.root.fon.voidClass;

/**
 *
 * @author sony
 */
public class Concrete {
    private FonResults FCV;
    private Vector<Object> ConcreteVoidVector;
    public Concrete()
    {
        this.FCV=null;
        this.ConcreteVoidVector=null;
    }
    public Concrete(FonResults fr)
    {
        this.FCV=fr;
        this.ConcreteVoidVector=null;
    }
    public ConcreteResults doConcrete() throws UnIntializedParameterException, ConcreteNotNeededException, NoMorePossibilitiesException, InvalidDimensionException, InvalidBoxEntryException
    {
        int x;
        if(this.FCV==null)
            throw new UnIntializedParameterException("\'Concrete.FCV\'");
        if(!this.FCV.ConcreteFlag)
            throw new ConcreteNotNeededException();
        if((x=this.test4Concrete())!=-1)
            throw new NoMorePossibilitiesException(x);
        
        this.ConcreteVoidVector=this.lcfVoidVectorConstructor();
        
        ConcreteAnalyser v=new ConcreteAnalyser(this.ConcreteVoidVector, this.FCV.FonSolutionBoard);
        return new ConcreteResults(v.doConcreteAnalysis(),this.FCV);
    }

    public static void printME(int[][]a)
    {
        System.out.println("Maruthi");
        for(int[]x:a)
        {
            for(int y:x)
                System.out.print(y+"\t");
            System.out.println();
        }
    }
    private int test4Concrete()
    {
        for(Object ob:this.FCV.FonAfterVoids)
            if(((voidClass)ob).getPossibilityCount()==0)
                return ((voidClass)ob).getMySquare();
        return -1;
    }

    //Constructing LCF void vector as a Priority Queue
    private Vector<Object> lcfVoidVectorConstructor() 
    {
        int i,j;
        Vector<Object> temp=new Vector<Object>();
        first:
            for(Object ob : this.FCV.FonAfterVoids)
            {
                j=((voidClass)ob).getPossibilityCount();
                for(i=0;i<temp.size();i++)
                    if(j<((voidClass)temp.get(i)).getPossibilityCount())
                    {
                        ((voidClass)ob).setPosList(((voidClass)ob).getOnlyPossList());
                        temp.insertElementAt(ob, i);
                        continue first;
                    }
                if(i==temp.size())
                {
                    ((voidClass)ob).setPosList(((voidClass)ob).getOnlyPossList());
                    temp.insertElementAt(ob, i);
                }
            }
        return temp;
    }
}
